---
sidebar_position: 1
---

# Defining custom tools

One option for creating a tool that runs custom code is to use a `DynamicTool`.

The `DynamicTool` and `DynamicStructuredTool` classes takes as input a name, a description, and a function.
Importantly, the name and the description will be used by the language model to determine when to call this function and with what parameters,
so make sure to set these to some values the language model can reason about!

The provided function is what will the agent will actually call. When an error occurs, the function should, when possible, return a string representing an error, rather than throwing an error.
This allows the error to be passed to the LLM and the LLM can decide how to handle it. If an error is thrown, then execution of the agent will stop.

`DynamicStructuredTool`s allow you to specify more complex inputs as [Zod](https://zod.dev) schemas for the agent to populate. However, note that more complex schemas require
better models and agents. See [this guide](/docs/modules/agents/agent_types) for a complete list of agent types.

See below for an example of defining and using `DynamicTool`s.

import CodeBlock from "@theme/CodeBlock";
import Example from "@examples/agents/custom_tool.ts";

import IntegrationInstallTooltip from "@mdx_components/integration_install_tooltip.mdx";

<IntegrationInstallTooltip></IntegrationInstallTooltip>

```bash npm2yarn
npm install @langchain/openai
```

<CodeBlock language="typescript">{Example}</CodeBlock>
